package com.ljkj.cordial.chat.plugins.receiver;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

/**
 * @description:
 * @author: Panwei
 * @date: 2020/1/7 16:45
 */
public class AsrPluginReceiver extends BroadcastReceiver {
//"com.ljkj.cordial.chat.plugins.receiver.ASR"
    private static final String ASR_RECEIVER_ACTION = "com.ljkj.cordial.chat.plugins.receiver.ASR";

    private static final String KEY_LOCAL_PATH = "asr_local_path";
    private static final String KEY_CODE = "asr_intent_code";
    private static final String KEY_CONTENT = "asr_intent_content";
    private static final String KEY_TAG = "asr_intent_tag";
    private static final String KEY_EXTRA = "asr_intent_extra";

    public static void executeAsr(Context context, String audioPath, String tag, String extra) {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.lanjiu.plugin", "com.lanjiu.plugin.network.asr.AsrPluginAct"));
        intent.putExtra(KEY_LOCAL_PATH, audioPath);
        intent.putExtra(KEY_TAG, tag);
        intent.putExtra(KEY_EXTRA, extra);
        context.startActivity(intent);
    }

    public static void registerAsr(Context context, OnAsrReceiveListener listener) {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ASR_RECEIVER_ACTION);
        context.registerReceiver(new AsrPluginReceiver(listener), intentFilter);
    }

    private OnAsrReceiveListener mListener;

    public AsrPluginReceiver(OnAsrReceiveListener mListener) {
        this.mListener = mListener;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (ASR_RECEIVER_ACTION.equals(intent.getAction())) {
            int code = intent.getIntExtra(KEY_CODE, -1);
            String result = intent.getStringExtra(KEY_CONTENT);
            String tag = intent.getStringExtra(KEY_TAG);
            String extra = intent.getStringExtra(KEY_EXTRA);
            if (mListener != null)
                mListener.OnAsrReceived(code, result, tag, extra);
        }
    }

    public interface OnAsrReceiveListener {
        void OnAsrReceived(int code, String result, String tag, String extra);
    }
}
